package com.cskaoyan.mall.service.lmpl;

import com.cskaoyan.mall.dao.MessageMapper;
import com.cskaoyan.mall.model.bo.message.MessageBo;
import com.cskaoyan.mall.model.po.LeaveMessage;
import com.cskaoyan.mall.model.vo.goods.GoodsReplyVo;
import com.cskaoyan.mall.model.vo.message.CustomerVo;
import com.cskaoyan.mall.model.vo.message.GoodsLeaveMessageVo;
import com.cskaoyan.mall.model.vo.message.LeaveMessageVo;
import com.cskaoyan.mall.model.vo.message.QuestionAnswerVo;
import com.cskaoyan.mall.service.MessageService;
import com.cskaoyan.mall.utils.MyBatisUtils;
import org.apache.ibatis.session.SqlSession;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

/**
 * @ClassName: MessageServiceImpl
 * @Description: TODO
 * @Auther 庄左
 * @Date 2022/7/2 14:10
 * @Version
 **/
public class MessageServiceImpl implements MessageService {
    @Override
    public List<LeaveMessageVo> selectMessageDetails() {
        SqlSession sqlSession = MyBatisUtils.getSqlSession();
        MessageMapper mapper = sqlSession.getMapper(MessageMapper.class);
        List<LeaveMessageVo> leaveMessages = mapper.selectMessageDetails();
        sqlSession.commit();
        sqlSession.close();
        return leaveMessages;
    }




    //========================通过id查找提问者针对那个商品提问，以及提问者的名字=====================//
    @Override
    public GoodsLeaveMessageVo selectGoodsName(Integer id) {
        SqlSession sqlSession = MyBatisUtils.getSqlSession();
        MessageMapper mapper = sqlSession.getMapper(MessageMapper.class);
        GoodsLeaveMessageVo goodsName = mapper.selectGoodsName(id);
        sqlSession.commit();
        sqlSession.close();
        return goodsName;
    }

    @Override
    public CustomerVo selectUserName(Integer id) {
        SqlSession sqlSession = MyBatisUtils.getSqlSession();
        MessageMapper mapper = sqlSession.getMapper(MessageMapper.class);
        CustomerVo userName = mapper.selectUserName(id);
        sqlSession.commit();
        sqlSession.close();
        return userName;
    }

    //=====================================================================================================//
    @Override
    public List<LeaveMessageVo> selectReplyMessageDetails() {
        SqlSession sqlSession = MyBatisUtils.getSqlSession();
        MessageMapper mapper = sqlSession.getMapper(MessageMapper.class);
        List<LeaveMessageVo> leaveMessages = mapper.selectMessageDetails();
        sqlSession.commit();
        sqlSession.close();
        return leaveMessages;
    }

    //对未回复的提问进行回复
    @Override
    public void updateReplyContent(MessageBo messageBo) {
        SqlSession sqlSession = MyBatisUtils.getSqlSession();
        MessageMapper mapper = sqlSession.getMapper(MessageMapper.class);
        String replyTime = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(new Date());
        mapper.updateReplyContent(new LeaveMessage(messageBo.getId(), messageBo.getcontent(),replyTime));
        sqlSession.commit();
        sqlSession.close();
    }

    //========================================================================//


    @Override
    public List<QuestionAnswerVo> selectQuestionAnswerById(Integer id) {
        SqlSession sqlSession = MyBatisUtils.getSqlSession();
        MessageMapper mapper = sqlSession.getMapper(MessageMapper.class);
        List<QuestionAnswerVo> messageVos = mapper.selectQuestionAnswerById(id);
        sqlSession.commit();
        sqlSession.close();
        return messageVos;
    }




}
